<?php
{% for fk1 in foreignKeys %}
{% for fk2 in foreignKeysClone %}
{% if fk1 != fk2 %}


#{{ fk1.getForeignObject() }}Bean  
const TABLENAME_{{ formatter.camelCaseToUpperCase(fk1.getForeignObject()) }}_{{ formatter.camelCaseToUpperCase(fk2.getForeignObject()) }} = '{{ fk1.getTable() }}';

#{{ fk1.getForeignObject() }}Catalog  

/**
 * Link a {{ fk1.getForeignObject() }} to {{ fk2.getForeignObject() }}
 * @param {{ fk1.getDataType() }} ${{ fk1.getPhpName() }}
 * @param {{ fk2.getDataType() }} ${{ fk2.getPhpName() }}
{% for field in nonForeignKeys %}
 * @param {{ field.getDataType() }} ${{ field.getPhpName() }}
{% endfor %}
 */
public function linkTo{{ fk2.getForeignObject() }}(${{ fk1.getPhpName() }}, ${{ fk2.getPhpName() }}{% for field in nonForeignKeys %}, ${{ field.getPhpName() }}{% endfor%})
{
    try
    {
        $this->unlinkFrom{{ fk2.getForeignObject() }}(${{ fk1.getPhpName() }}, ${{ fk2.getPhpName() }});
        $data = array(
            '{{ fk1.getSimpleName() }}' => ${{ fk1.getPhpName() }},
            '{{ fk2.getSimpleName() }}' => ${{ fk2.getPhpName() }},
{% for field in nonForeignKeys %}
            '{{ field.getSimpleName() }}' => ${{ field.getPhpName() }},
{% endfor%}
        );
        $this->db->insert({{ fk1.getForeignObject() }}::TABLENAME_{{ formatter.camelCaseToUpperCase(fk1.getForeignObject()) }}_{{ formatter.camelCaseToUpperCase(fk2.getForeignObject()) }}, $data);
    }
    catch(Exception $e)
    {
        throw new {{ fk1.getForeignObject() }}Exception("The {{ fk1.getForeignObject() }} can't be saved \n" . $e->getMessage());
    }
}

/**
 * Unlink a {{ fk1.getForeignObject() }} from {{ fk2.getForeignObject() }}
 * @param {{ fk1.getDataType() }} ${{ fk1.getPhpName() }}
 * @param {{ fk2.getDataType() }} ${{ fk2.getPhpName() }}
 */
public function unlinkFrom{{ fk2.getForeignObject() }}(${{ fk1.getPhpName() }}, ${{ fk2.getPhpName() }})
{
    try
    {
        $where = array(
            $this->db->quoteInto('{{ fk1.getSimpleName() }} = ?', ${{ fk1.getPhpName() }}),
            $this->db->quoteInto('{{ fk2.getSimpleName() }} = ?', ${{ fk2.getPhpName() }}),
        );
        $this->db->delete({{ fk1.getForeignObject() }}::TABLENAME_{{ formatter.camelCaseToUpperCase(fk1.getForeignObject()) }}_{{ formatter.camelCaseToUpperCase(fk2.getForeignObject()) }}, $where);
    }
    catch(Exception $e)
    {
        throw new {{ fk1.getForeignObject() }}Exception("The {{ fk1.getForeignObject() }} can't be erased \n" . $e->getMessage());
    }
}

/**
 * Unlink all {{ fk2.getForeignObject() }} relations
 * @param {{ fk1.getDataType() }} ${{ fk1.getPhpName() }}
{% for field in nonForeignKeys %}
 * @param {{ field.getDataType() }} ${{ field.getPhpName() }}
{% endfor %}
 */
public function unlinkAll{{ fk2.getForeignObject() }}(${{ fk1.getPhpName() }}{% for field in nonForeignKeys %}, ${{ field.getPhpName() }} = null{% endfor%})
{
    try
    {
        $where = array(
            $this->db->quoteInto('{{ fk1.getSimpleName() }} = ?', ${{ fk1.getPhpName() }}),
        );
{% for field in nonForeignKeys %}
        if(null != ${{ field.getPhpName() }}) $where[] = $this->db->quoteInto('{{ field.getSimpleName() }} = ?', ${{ field.getPhpName() }});
{% endfor %}
        $this->db->delete({{ fk1.getForeignObject() }}::TABLENAME_{{ formatter.camelCaseToUpperCase(fk1.getForeignObject()) }}_{{ formatter.camelCaseToUpperCase(fk2.getForeignObject()) }}, $where);
    }
    catch(Exception $e)
    {
        throw new {{ fk1.getForeignObject() }}Exception("The {{ fk1.getForeignObject() }} can't be erased \n" . $e->getMessage());
    }
}

/**
 * Get {{ fk1.getForeignObject() }} - {{ fk2.getForeignObject() }} relations by Criteria
 * @param Criteria $criteria
 * @return array 
 */
public function get{{ fk1.getForeignObject() }}{{ fk2.getForeignObject() }}(Criteria $criteria = null)
{ 
    $criteria = (null === $criteria) ? new Criteria() : $criteria;
    $this->db->setFetchMode(Zend_Db::FETCH_ASSOC);
    try
    {
       $sql = "SELECT * FROM ". {{ fk1.getForeignObject() }}::TABLENAME_{{ formatter.camelCaseToUpperCase(fk1.getForeignObject()) }}_{{ formatter.camelCaseToUpperCase(fk2.getForeignObject()) }} ."
               WHERE  " . $criteria->createSql();
       $result = $this->db->fetchAll($sql);
    } catch(Exception $e)
    {
       throw new {{ fk1.getForeignObject() }}Exception("No se pudieron obtener las relaciones\n" . $e->getMessage());
    }
    return $result;
}

/**
 * Get a {{ fk1.getForeignObject() }}Collection by {{ fk2.getForeignObject() }}
 * @param {{ fk2.getDataType() }} ${{ fk2.getPhpName() }}
{% for field in nonForeignKeys %}
 * @param [{{ field.getDataType() }}|array] ${{ field.getPhpName() }}
{% endfor %}
 * @return {{ fk1.getForeignObject() }}Collection
 */
public function getBy{{ fk2.getForeignObject() }}(${{ fk2.getPhpName() }}{% for field in nonForeignKeys %}, ${{ field.getPhpName() }} = null{% endfor%})
{
    $criteria = new Criteria();
    $criteria->add('{{ fk2.getSimpleName() }}', ${{ fk2.getPhpName() }}, Criteria::EQUAL);
{% for field in nonForeignKeys %}
    if(null != ${{ field.getPhpName() }}) $criteria->add('{{ field.getSimpleName() }}', ${{ field.getPhpName() }}, is_array(${{ field.getPhpName() }}) ? Criteria::IN : Criteria::EQUAL);
{% endfor %}
    ${{ fk1.getForeignLowerObject() }}{{ fk2.getForeignObject() }} = $this->get{{ fk1.getForeignObject() }}{{ fk2.getForeignObject() }}($criteria);
    $ids = array();
    foreach(${{ fk1.getForeignLowerObject() }}{{ fk2.getForeignObject() }} as $rs){
        $ids[] = $rs['{{ fk1.getSimpleName() }}'];
    }
    return $this->getByIds($ids);
}

{% endif %}
{% endfor %}
{% endfor %}


